<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Loads extends CI_Controller {
	     public function __construct()
       {
            parent::__construct();
		$this->load->helper("html");
		$this->load->helper("url");
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model("loads_model");
		$this->load->model("dv_payments_model");
		$this->load->model("dv_model");
		//$this->load->model("guest_comment_model");
		//$this->load->model("news_events_model");
		//$this->load->model("roomcottages_model");
		$this->load->helper(array('form', 'url'));
		$this->load->database();
	}
	//START ------------------Loads---------------------
	function index()
	{
		$data['title']="Administrator Page";
		$data["list_loads"] = $this->loads_model->get_all_loads();	
		$this->load->view('loads_list_view',$data);
	}
	
	function SearchLoads()
	{
	$column_value=$_POST['column_value'];
	$data["column_value"]=$column_value;
	$data["list_loads"]=$this->loads_model->getSearchLoads($_POST['search_value'],$column_value);
	$this->load->view('loads_list_view',$data);
	}
	function view_loads()
	{
		 $code = $this->uri->segment(3);
		$data["info"] = $this->loads_model->getloads_info($code);	
		$this->load->view('loads_file_view',$data);
	}
		
	function add_loads()

	{
		$data['title']="Administrator Page";
		$code = $this->uri->segment(3);
		$data["list_loads"] = $this->loads_model->getloads_info($code);		
		$this->load->view('loads_add_view',$data);
	}
	function p_getinfo()
	{		
		$info = $this->dv_model->getatInfoby_dv_company_name($_GET['dv_company_name']);			
		$this->output->set_content_type('application/json');
    	$this->output->set_output(json_encode($info));
	}
	
	function p_add_loads()

	{
	if(isset($_POST['cmdUpload']))
		{
			$this->load->database();
			$config['upload_path'] = 'upload';
			$config['allowed_types'] = 'gif|jpg|png|pdf|one';
			$config['max_size']	= '';
			$config['max_width']  = '';
			$config['max_height']  = '';
			$config['encrypt_name'] = FALSE;
			$config['overwrite'] = FALSE;
			$config['remove_spaces'] = true;
			
			//echo"ni sulod sa cmd upload";
			
			$this->load->library('upload', $config);
		
			if ( ! $this->upload->do_upload())
			{
				$error = array('error' => $this->upload->display_errors());
						$file_name = 0;	

			}	
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$file_name = $this->upload->file_name;
			}
//		$file_name = 0;	
		$pdf = $file_name;	
		$path = my_url()."upload/$pdf";
		
		$load_no =$_POST['load_no'];
		$date =$_POST['date'];
		$rep =$_POST['rep'];
		$bc_company_name =$_POST['bc_company_name'];
		$bc_confrm_no =$_POST['bc_confrm_no'];
		$bc_rate =$_POST['bc_rate'];
		$pick_up_place =$_POST['pick_up_place'];		
		$pick_up_city =$_POST['pick_up_city'];
		$pick_up_state =$_POST['pick_up_state'];
		$pick_up_date =$_POST['pick_up_date'];			
		$drop_off_place =$_POST['drop_off_place'];		
		$drop_off_city =$_POST['drop_off_city'];
		$drop_off_state =$_POST['drop_off_state'];
		$drop_off_date =$_POST['drop_off_date'];
		$dv_company_name=$_POST['dv_company_name'];
		$contract_amount =$_POST['contract_amount'];
		$f_name=$_POST['f_name'];
		$cell_phone_num=$_POST['cell_phone_num'];
		$mc_num=$_POST['mc_num'];
		$type_of_truck =$_POST['type_of_truck'];		
		$comment_box =$_POST['comment_box'];
		
		$loadinfo = $this->loads_model->getloadInfoByLoadNo($load_no);	

		if($loadinfo==null){							 
		$_sql="insert into loads(load_no,date,rep,bc_company_name,bc_confrm_no,bc_rate,pick_up_place,pick_up_city,pick_up_state,pick_up_date,drop_off_place,drop_off_city,drop_off_state,drop_off_date,dv_company_name,contract_amount,f_name,cell_phone_num,mc_num,type_of_truck,comment_box,file_path)";
			$_sql.="values('$load_no','$date','$rep','$bc_company_name','$bc_confrm_no','$bc_rate','$pick_up_place','$pick_up_city','$pick_up_state','$pick_up_date','$drop_off_place','$drop_off_city','$drop_off_state','$drop_off_date','$dv_company_name','$contract_amount','f_name','cell_phone_num','$mc_num','$type_of_truck','$comment_box','$$path')";
			$query = $this->db->query($_sql); 			
		
		if($query)
				{
				$data['recordflag']="<img src='".my_url()."images/icons/success.gif' />
				Successfully Added.";
				}
				else{
				$data['recordflag']="<img src='".my_url()."images/icons/error.gif' />
				 Error on Saving Data.";
				}


			$data["list_loads"] = $this->loads_model->get_all_loads();	
			$this->load->view('loads_list_view',$data);

		}
}
		else{			

			$data["data_exist"] = "[$load_no] - Load No. already exist.";	

			$this->load->view('loads_add_view',$data);		

		}	
		
		$paymentinfo = $this->dv_payments_model->getpaymentInfoBypaymentNo($load_no);	
		if($paymentinfo==null){
				
		$_sql="INSERT into vendor_payments(load_no,dv_company_name,contract_amount) values('$load_no','$dv_company_name','$contract_amount')";
		$query = $this->db->query($_sql); 

		}

		else{			

			$data["data_exists"] = "[$load_no] - Load No. already exist.";	

			$this->load->view('dv_payments_add_view',$data);		

		}	

		$_sql="INSERT into available_truck(load_no,cell_phone_num,f_name,dv_company_name,drop_off_city,drop_off_state,drop_off_date,type_of_truck,mc_num)
		 values('$load_no','$cell_phone_num','$f_name','$dv_company_name','$drop_off_city','$drop_off_state','$drop_off_date','$type_of_truck','$mc_num')";
		$query = $this->db->query($_sql); 
		
		$_sql="INSERT into at_new(load_no,cell_phone_num,f_name,dv_company_name,drop_off_city,drop_off_state,drop_off_date,type_of_truck,mc_num) 
		values		
		('$load_no','$cell_phone_num','$f_name','$dv_company_name','$drop_off_city','$drop_off_state','$drop_off_date','$type_of_truck','$mc_num')";
		$query = $this->db->query($_sql);
		 
	
	}
	function edit_loads()

	{

 	    $code = $this->uri->segment(3);
		$data["info"] = $this->loads_model->getloads_info($code);	
		$this->load->view('loads_edit_view',$data);
	}

	function p_edit_loads()

	{
	if(isset($_POST['cmdUpload']))
		{
			$this->load->database();
			$config['upload_path'] = 'upload';
			$config['allowed_types'] = 'gif|jpg|png|pdf|one';
			$config['max_size']	= '';
			$config['max_width']  = '';
			$config['max_height']  = '';
			$config['encrypt_name'] = FALSE;
			$config['overwrite'] = FALSE;
			$config['remove_spaces'] = true;
			
			//echo"ni sulod sa cmd upload";
			
			$this->load->library('upload', $config);
		
			if ( ! $this->upload->do_upload())
			{
				$error = array('error' => $this->upload->display_errors());
				$file_name = 0;	
				
			}	
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$file_name = $this->upload->file_name;
			}
		$pdf = $file_name;	
		$path = my_url()."upload/$pdf";
	
		$loads_id =$_POST['hid_loadsid'];
		$load_no =$_POST['load_no'];
		$date =$_POST['date'];
		$rep =$_POST['rep'];
		$bc_company_name =$_POST['bc_company_name'];
		$bc_confrm_no =$_POST['bc_confrm_no'];
		$bc_rate =$_POST['bc_rate'];
		$pick_up_place =$_POST['pick_up_place'];		
		$pick_up_city =$_POST['pick_up_city'];
		$pick_up_state =$_POST['pick_up_state'];
		$pick_up_date =$_POST['pick_up_date'];			
		$drop_off_place =$_POST['drop_off_place'];		
		$drop_off_city =$_POST['drop_off_city'];
		$drop_off_state =$_POST['drop_off_state'];
		$drop_off_date =$_POST['drop_off_date'];
		$dv_company_name=$_POST['dv_company_name'];
		$contract_amount =$_POST['contract_amount'];
		$f_name=$_POST['f_name'];
		$cell_phone_num =$_POST['cell_phone_num'];		
		$mc_num=$_POST['mc_num'];
		$type_of_truck =$_POST['type_of_truck'];				
		$comment_box =$_POST['comment_box'];
			$query = $this->db->query("UPDATE loads SET 
											  		      load_no='$load_no',
														  date='$date',
														  rep='$rep',
														  bc_company_name='$bc_company_name',
														  bc_confrm_no='$bc_confrm_no',														
														  bc_rate='$bc_rate',
														  pick_up_place='$pick_up_place',														
														  pick_up_city='$pick_up_city',
														  pick_up_state='$pick_up_state',
														  pick_up_date='$pick_up_date',
														  drop_off_place='$drop_off_place',														
														  drop_off_city='$drop_off_city',
														  drop_off_state='$drop_off_state',
														  drop_off_date='$drop_off_date',
														  dv_company_name='$dv_company_name',	
														  contract_amount='$contract_amount',															
														  f_name='$f_name',
														  cell_phone_num='$cell_phone_num',
														  mc_num='$mc_num',
 														  type_of_truck ='$type_of_truck',
														  comment_box='$comment_box',
														  file_path='$path'
														   where loads_id= $loads_id"); 		


		if($query)
				{
				$data['recordflag']="<img src='".my_url()."images/icons/success.gif' />
				Successfully Added.";
				}
				else{
				$data['recordflag']="<img src='".my_url()."images/icons/error.gif' />
				 Error on Saving Data.";
				}

				  	
$query = $this->db->query("UPDATE vendor_payments SET 

														  dv_company_name='$dv_company_name',
														  contract_amount='$contract_amount' where load_no= $load_no"); 		
$query = $this->db->query("UPDATE available_truck SET 
														  cell_phone_num='$cell_phone_num',
														  f_name='$f_name',
														  dv_company_name='$dv_company_name',
														  drop_off_city='$drop_off_city',
														  drop_off_state='$drop_off_state',
														  type_of_truck='$type_of_truck',
														  mc_num='$mc_num' where load_no= $load_no"); 	

$query = $this->db->query("UPDATE at_new SET 
														  cell_phone_num='$cell_phone_num',
														  f_name='$f_name',
														  dv_company_name='$dv_company_name',
														  drop_off_city='$drop_off_city',
														  drop_off_state='$drop_off_state',
														  type_of_truck='$type_of_truck',
														  mc_num='$mc_num' where load_no= $load_no"); 	
														  
		$data["list_loads"] = $this->loads_model->get_all_loads();	
		$this->load->view('loads_list_view',$data);		
	}
	}
	function delete_loads()
	{
		$this->load->database();		
		$code = $this->uri->segment(3);
		$query = $this->db->query("delete from loads where loads_id='$code'");
		$data["list_loads"] = $this->loads_model->get_all_loads();	
		$this->load->view('loads_list_view',$data);
	}
	//END ------------------Loads---------------------
	function logout()
	{
		//$this->load->library('session');
		//$this->session->sess_destroy();
		session_start();
		session_destroy();
		//echo $_SESSION['Administrator_log'];
		//unset($_SESSION['Administrator_log']);
		//$data["newsresult"] = $this->loads_model->dv_all_driver_vendor();
			$this->load->view('admin_view');

	}
}

?>